home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / FONTCDLG.ZIP / FONTCDLG.CPP next >
C/C++ Source or Header  |  1992-03-21  |  10KB  |  195 lines

  1. //=======================================================================//
  2. // FONTCDLG.CPP released to the public domain 6/6/92 by Bob Bourbonnais  //
  3. //        Demonstrates using a Windows 3.1 Font Common Dialog Box        //
  4. //             with C++ and the Object Windows Class Library             //
  5. //=======================================================================//
  6. #include <owl.h>        // for Object Windows
  7. #include <static.h>     // for static control
  8. #include <dialog.h>     // for dialog boxes
  9. #include <commdlg.h>    // for Win 3.1 Common Dialogs
  10. #include "fontcdlg.h"
  11. //=======================================================================//
  12. _CLASSDEF(TFontDialog)               // Class to encapsulate
  13. class TFontDialog : public TDialog   // the Windows 3.1
  14.   {                                  // Font Common Dialog
  15.   public:
  16.     TFontDialog(PTWindowsObject AParent, // Constructor
  17.          LOGFONT far * lfNewFont, DWORD * dwNewColor,
  18.          LPSTR lpName, PTModule AModule);
  19.     virtual BOOL Create();            // Create for non-modal Creation
  20.     virtual int Execute();            // Execute for modal Execution
  21.     #pragma argsused                  // Ignore ARetValue non use
  22.     virtual void CloseWindow(int ARetValue){}; // trap destruction
  23.   protected:
  24.     DWORD * dwColor;                 // Font Color
  25.     CHOOSEFONT fcdlg;                // Font Common Dialog Structure
  26.   };
  27. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
  28. TFontDialog::TFontDialog(PTWindowsObject AParent, LOGFONT far * lfNewFont,
  29.              DWORD * dwNewColor, LPSTR AName,
  30.              PTModule AModule = NULL)
  31.          :TDialog(AParent,AName,AModule) // Constructor calls base
  32.   {
  33.   dwColor = dwNewColor;     // equate color address to allow updateing
  34.   fcdlg.hDC            = HDC(NULL);             // used for printer fonts
  35.   fcdlg.lStructSize    = sizeof(CHOOSEFONT);    // initialize ccdl struct
  36.   fcdlg.hwndOwner      = GetFocus();            // parent has the focus
  37.   fcdlg.lpLogFont      = lfNewFont;             // LOGFONT structure
  38.   fcdlg.Flags          = CF_SCREENFONTS | CF_EFFECTS; // no printer fonts
  39.   fcdlg.rgbColors      = * dwNewColor;          // Font Color selection
  40.   fcdlg.lCustData      = 0L;                    // Use Default Data
  41.   fcdlg.lpfnHook       = (FARPROC)NULL;         // No Message trapping
  42.   fcdlg.lpTemplateName = (LPSTR)NULL;           // Use Default dialog
  43.   fcdlg.hInstance      = (HANDLE) NULL;         // Not Used
  44.   fcdlg.lpszStyle      = (LPSTR)NULL;           // Used with CF_USESTYLE
  45.   fcdlg.nFontType      = SCREEN_FONTTYPE;       // Screen Font
  46.   fcdlg.nSizeMin       = 0;                     // Used with CF_LIMITSIZE
  47.   fcdlg.nSizeMax       = 0;                     // Used with CF_LIMITSIZE
  48.   };
  49. BOOL TFontDialog::Create()                      // Called by MakeWindow
  50.   {
  51.   fcdlg.hwndOwner = NULL;                       // No parent for non-Model
  52.   ChooseFont(&fcdlg);                           // fcdlg function call
  53.   *dwColor = fcdlg.rgbColors;                   // update color variable
  54.   return 1;                                     // Success
  55.   }
  56. int TFontDialog::Execute()                      // Called by CreateDialog
  57.   {
  58.   ChooseFont(&fcdlg);                           // fcdlg function call
  59.   *dwColor = fcdlg.rgbColors;                   // update color variable
  60.   return 1;                                     // Success
  61.   }
  62. //=======================================================================//
  63. _CLASSDEF(TMyFontStatic)                 // Class derived from a static
  64. class TMyFontStatic : public TStatic
  65.   {
  66.   public:
  67.     TMyFontStatic(PTWindowsObject AParent, int AnId,  // constructor
  68.          LOGFONT * lfInitFont, DWORD dwInitColor,
  69.          int X,int Y,int W,int H,
  70.          LPSTR ATitle, WORD ATextLen,
  71.          PTModule AModule);
  72.     virtual void ChangeFontIndirect(LOGFONT lfNewFont, // Changes Font
  73.                     DWORD dwNewColor); // and Font Color
  74.   protected:
  75.     char szMyText[80];                                 // Display Text
  76.     HFONT  hFontOld;                                   // Handle to old font
  77.     DWORD   dwFontColor;                               // Font Color
  78.     LOGFONT lfDisplay;                                 // Font Structure
  79.     virtual void WMPaint(RTMessage Msg);               // traps paint message
  80.   };
  81. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
  82. TMyFontStatic::TMyFontStatic(PTWindowsObject AParent, int AnId,
  83.                LOGFONT * lfInitFont,DWORD dwInitColor,
  84.                int X, int Y, int W, int H,
  85.                LPSTR ATitle = "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
  86.                WORD ATextLen = 26, PTModule AModule = NULL)
  87.         :TStatic(AParent, AnId, "", X,Y,W,H, ATextLen, AModule)
  88.   {
  89.   lstrcpy(szMyText,ATitle);         // Copies the Title into output string
  90.   lfDisplay = *lfInitFont;          // initializes font type structure
  91.   dwFontColor = dwInitColor;        // and font color
  92.   }
  93. void TMyFontStatic::ChangeFontIndirect(LOGFONT lfNewFont,DWORD dwNewColor)
  94.   {
  95.   lfDisplay = lfNewFont;            // updates font structure
  96.   dwFontColor = dwNewColor;         // and font color
  97.   InvalidateRect(HWindow,NULL,TRUE);// then forces redraw of control
  98.   }
  99. void TMyFontStatic::WMPaint(RTMessage Msg) // intercept WM_Paint message
  100.   {
  101.   TStatic::WMPaint(Msg);                        // pass along to base class
  102.   HDC hDC     = GetDC(HWindow);                 // grab the Device Contect
  103.   HFONT hFont = CreateFontIndirect(&lfDisplay); // create a new font
  104.   hFontOld    = SelectObject(hDC, hFont);       // select it into display
  105.   SetTextColor(hDC,dwFontColor);                // set the text color
  106.   SetBkMode (hDC, TRANSPARENT);                 // Text Background
  107.   TextOut(hDC,0,0,szMyText,lstrlen(szMyText));  // Output string
  108.   SelectObject(hDC,hFontOld);                   // restore old font
  109.   DeleteObject(hFont);                          // delete font handle
  110.   ReleaseDC(HWindow,hDC);                       // release the DC
  111.   }
  112. //=======================================================================//
  113. _CLASSDEF(TMainDialog)                   // Class derived from a dialog
  114. class TMainDialog : public TDialog       // to add menu and button
  115.   {                     // processing to a main dialog
  116.   public:
  117.     PTMyFontStatic pMyFontControl;      // Pointer for Font Control
  118.     TMainDialog(LPSTR lpName);            // constructor
  119.     virtual void HandleMenuItem(RTMessage Msg)      // menu handler
  120.       = [CM_FIRST + IDM_FONT];
  121.     virtual void HandleButtonMessage(RTMessage Msg) // button handler
  122.       = [ID_FIRST + IDB_FONT];
  123.   protected:
  124.     DWORD dwFontColor;                              // font color
  125.     LOGFONT lfFont;                                 // font structure
  126.   };
  127. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
  128. TMainDialog::TMainDialog(LPSTR lpName)              // constructor
  129.       :TDialog(NULL,lpName)                     // calls base class
  130.   {
  131.   dwFontColor = RGB(0,0,255);                       // blue text
  132.   lfFont.lfHeight = 20;                             // helvetica font
  133.   lfFont.lfWidth  = 8;
  134.   lfFont.lfEscapement = 0;
  135.   lfFont.lfOrientation = 0;
  136.   lfFont.lfWeight = 400;
  137.   lfFont.lfItalic = 0;
  138.   lfFont.lfUnderline = 0;
  139.   lfFont.lfStrikeOut = 0;
  140.   lfFont.lfCharSet = ANSI_CHARSET;
  141.   lfFont.lfQuality = DEFAULT_QUALITY;
  142.   lfFont.lfPitchAndFamily = VARIABLE_PITCH | FF_DONTCARE;
  143.   lstrcpy(lfFont.lfFaceName, "Helv");
  144.  
  145.   pMyFontControl = new TMyFontStatic(this,ID_RECT,&lfFont, // create new
  146.                      dwFontColor,          // static control
  147.                      22,14,330,120);
  148.   }
  149. void TMainDialog:: HandleMenuItem(RTMessage)          // menu item activates
  150.   {                                                   // non-modal common
  151.   GetApplication()->MakeWindow(new TFontDialog(this,  // font dialog box
  152.                    &lfFont,&dwFontColor,  // to get font info
  153.                    "Main_Window_Dialog"));
  154.   pMyFontControl->ChangeFontIndirect(lfFont,dwFontColor); // change font
  155.   }
  156. void TMainDialog:: HandleButtonMessage(RTMessage)     // button activates
  157.   {                                                   //